﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace eShop.Business
{
    public class SEOHelper
    {
        #region Methods
        /// <summary>
        /// Renders page meta tag
        /// </summary>
        /// <param name="page">Page instance</param>
        /// <param name="name">Meta name</param>
        /// <param name="content">Content</param>
        /// <param name="OverwriteExisting">Overwrite existing content if exists</param>
        public static void RenderMetaTag(Page page, string name, string content, bool OverwriteExisting)
        {
            if (page == null || page.Header == null)
                return;
            bool Flag = false;
            foreach (Control control in page.Header.Controls)
                if (control is HtmlMeta)
                {
                    HtmlMeta meta = (HtmlMeta)control;
                    if (meta.Name.ToLower().Equals(name.ToLower()) && !string.IsNullOrEmpty(content))
                    {
                        Flag = true;
                        if (OverwriteExisting)
                            meta.Content = content;
                        else
                        {
                            if (String.IsNullOrEmpty(meta.Content))
                                meta.Content = content;
                        }
                    }
                }
            if (Flag == false)
            {
                //HtmlMeta metaKeywords = new HtmlMeta();
                //page.Header.Controls.Add((new LiteralControl("\n")));
                //metaKeywords.Name = name;
                //metaKeywords.Content = content;
                //page.Header.Controls.Add(metaKeywords);
            }
        }


        /// <summary>
        /// Renders page title
        /// </summary>
        /// <param name="page">Page instance</param>
        /// <param name="title">Page title</param>
        /// <param name="OverwriteExisting">Overwrite existing content if exists</param>
        public static void RenderTitle(Page page, string title, bool OverwriteExisting)
        {
            if (page == null || page.Header == null)
                return;

            if (String.IsNullOrEmpty(title))
                return;

            if (OverwriteExisting)
                page.Title = title;
            else
            {
                if (String.IsNullOrEmpty(page.Title))
                    page.Title = title;
            }
        }
        #endregion

    }

}
